package com.huawei.hms.support.api.iap.json;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import com.huawei.hms.api.Api;
import com.huawei.hms.api.HuaweiApiAvailability;
import com.huawei.hms.common.HuaweiApi;
import com.huawei.hms.core.aidl.IMessageEntity;
import com.huawei.hms.support.api.entity.iap.ConsumePurchaseReq;
import com.huawei.hms.support.api.entity.iap.GetBuyIntentReq;
import com.huawei.hms.support.api.entity.iap.GetBuyIntentWithPriceReq;
import com.huawei.hms.support.api.entity.iap.GetPurchaseReq;
import com.huawei.hms.support.api.entity.iap.OrderServiceNaming;
import com.huawei.hms.support.api.entity.iap.SkuDetailReq;
import com.huawei.hms.support.api.iap.BuyResultInfo;
import com.huawei.hms.support.api.iap.ConsumePurchaseResult;
import com.huawei.hms.support.api.iap.GetBuyIntentResult;
import com.huawei.hms.support.api.iap.GetPurchasesResult;
import com.huawei.hms.support.api.iap.IsBillingSupportedResult;
import com.huawei.hms.support.api.iap.SkuDetailResult;
import com.huawei.hms.support.hianalytics.HiAnalyticsClient;
import com.huawei.hms.support.hianalytics.HiAnalyticsConstant.KeyAndValue;
import com.huawei.hms.support.hianalytics.HiAnalyticsUtil;
import com.huawei.hms.support.log.HMSLog;
import com.huawei.hms.utils.Checker;
import com.huawei.hms.utils.JsonUtil;
import java.security.SecureRandom;
import java.util.Locale;
import o.fjo;

public class IapClientImpl extends HuaweiApi<IapOptions> implements IapClient {
    private static final Api<IapOptions> a = new Api(HuaweiApiAvailability.HMS_API_NAME_IAP);
    private static IapOptions b = new IapOptions();

    IapClientImpl(Activity activity) {
        super(activity, a, b, new IapHmsClientBuilder());
    }

    IapClientImpl(Context context) {
        super(context, a, b, new IapHmsClientBuilder());
    }

    private static String a() {
        String str = System.currentTimeMillis();
        SecureRandom secureRandom = new SecureRandom();
        return str + String.format(Locale.ROOT, "%06d", new Object[]{Integer.valueOf(secureRandom.nextInt(1000000))});
    }

    public fjo<SkuDetailResult> getSkuDetail(SkuDetailReq skuDetailReq) {
        Checker.checkNonNull(skuDetailReq);
        return doWrite(new SkuDetailTaskApiCall(OrderServiceNaming.GET_SKU_DETAILS, JsonUtil.createJsonString(skuDetailReq), HiAnalyticsClient.reportEntry(getContext(), OrderServiceNaming.GET_SKU_DETAILS)));
    }

    public fjo<ConsumePurchaseResult> consumePurchase(ConsumePurchaseReq consumePurchaseReq) {
        Checker.checkNonNull(consumePurchaseReq);
        return doWrite(new ConsumePurchaseTaskApiCall(OrderServiceNaming.CONSUME_PURCHASE, JsonUtil.createJsonString(consumePurchaseReq), HiAnalyticsClient.reportEntry(getContext(), OrderServiceNaming.CONSUME_PURCHASE)));
    }

    public fjo<GetPurchasesResult> getPurchases(GetPurchaseReq getPurchaseReq) {
        Checker.checkNonNull(getPurchaseReq);
        return doWrite(new GetPurchasesTaskApiCall(OrderServiceNaming.GET_PURCHASE, JsonUtil.createJsonString(getPurchaseReq), HiAnalyticsClient.reportEntry(getContext(), OrderServiceNaming.GET_PURCHASE)));
    }

    public fjo<GetPurchasesResult> getPurchaseHistory(GetPurchaseReq getPurchaseReq) {
        Checker.checkNonNull(getPurchaseReq);
        return doWrite(new GetPurchasesTaskApiCall(OrderServiceNaming.GET_PURCHASE_HISTORY, JsonUtil.createJsonString(getPurchaseReq), HiAnalyticsClient.reportEntry(getContext(), OrderServiceNaming.GET_PURCHASE_HISTORY)));
    }

    public fjo<GetBuyIntentResult> getBuyIntentWithPrice(GetBuyIntentWithPriceReq getBuyIntentWithPriceReq) {
        Checker.checkNonNull(getBuyIntentWithPriceReq);
        IMessageEntity getBuyIntentWithPriceReq2 = new GetBuyIntentWithPriceReq(getBuyIntentWithPriceReq, a());
        HiAnalyticsUtil.getInstance().onEvent(getContext(), KeyAndValue.START_BUYWITHPRICE, getBuyIntentWithPriceReq2.getTransactionId());
        return doWrite(new GetBuyIntentTaskApiCall(OrderServiceNaming.BUY_WITH_PRICE, JsonUtil.createJsonString(getBuyIntentWithPriceReq2), HiAnalyticsClient.reportEntry(getContext(), OrderServiceNaming.BUY_WITH_PRICE)));
    }

    public fjo<GetBuyIntentResult> getBuyIntent(GetBuyIntentReq getBuyIntentReq) {
        String reportEntry;
        Checker.checkNonNull(getBuyIntentReq);
        Object getBuyIntentReq2 = new GetBuyIntentReq(getBuyIntentReq, a());
        if (getBuyIntentReq.priceType == 2) {
            reportEntry = HiAnalyticsClient.reportEntry(getContext(), OrderServiceNaming.BUY);
            HiAnalyticsUtil.getInstance().onEvent(getContext(), KeyAndValue.START_SUB, getBuyIntentReq2.getTransactionId());
        } else {
            reportEntry = HiAnalyticsClient.reportEntry(getContext(), OrderServiceNaming.BUY);
            HiAnalyticsUtil.getInstance().onEvent(getContext(), KeyAndValue.START_BUY, getBuyIntentReq2.getTransactionId());
        }
        return doWrite(new GetBuyIntentTaskApiCall(OrderServiceNaming.BUY, JsonUtil.createJsonString(getBuyIntentReq2), reportEntry));
    }

    public BuyResultInfo getBuyResultInfoFromIntent(Intent intent) {
        if (intent == null) {
            intent = new Intent();
        }
        Bundle extras = intent.getExtras();
        if (extras == null) {
            extras = new Bundle();
        }
        BuyResultInfo buyResultInfo = new BuyResultInfo();
        buyResultInfo.setReturnCode(extras.getInt("returnCode", 1));
        buyResultInfo.setErrMsg(extras.getString("errMsg"));
        buyResultInfo.setInAppPurchaseData(extras.getString("inAppPurchaseData"));
        buyResultInfo.setInAppDataSignature(extras.getString("Signature"));
        HMSLog.i("IapClientImpl", "getBuyResultInfoFromIntent:" + buyResultInfo.getReturnCode());
        return buyResultInfo;
    }

    public fjo<IsBillingSupportedResult> isBillingSupported() {
        return doWrite(new IsBillingSupportedTaskApiCall(OrderServiceNaming.IS_BILLING_SUPPORTED, HiAnalyticsClient.reportEntry(getContext(), OrderServiceNaming.IS_BILLING_SUPPORTED)));
    }
}
